home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / SFINGR.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  8KB  |  315 lines

  1. /*
  2. **  SFINGR.C
  3. **
  4. **  Synchronous FINGER program.
  5. **  Compare to AFINGR and FINGER.
  6. */
  7.  
  8. #include <windows.h>
  9. #include <winsock.h>
  10.  
  11. #include "wil.h"
  12. #include "message.h"
  13. #include "paint.h"
  14. #include "about.h"
  15. #include "str.h"
  16.  
  17.  
  18. #ifdef WIN32
  19. #define USE_INS HINSTANCE
  20. #define USE_PTR PSTR
  21. #else
  22. #define USE_INS HANDLE
  23. #define USE_PTR LPSTR
  24. #endif
  25.  
  26. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  27.  
  28. /* globals */
  29.  
  30. HWND hMainWnd;            /* main window handle */
  31.  
  32. #define BS            8
  33. #define LF           10
  34. #define CR           13
  35. #define MAX_BUF     128
  36. #define MAX_STR      40
  37.  
  38. #define ONE_SEC    1000
  39. #define TEN_SEC   10000
  40.  
  41. #define SFINGR_PORT  79
  42.  
  43. static HMENU hMenu;
  44. static USE_INS hInstance;
  45. static int WinWidth = 8 * NCOLS;
  46. static int WinHeight = 12 * NROWS + 48;
  47. static char Temp[MAX_BUF+8];
  48. static int  InBufLen = 0;
  49. static char InBuffer[MAX_BUF+1];
  50. static char User[MAX_STR] = "";
  51. static char Host[MAX_STR] = "";
  52. static SOCKET Socket = 0;
  53. static ULONG  HostAddr = 0;
  54. static LPSTR  Ptr;
  55. static HCURSOR ArrowCursor;
  56. static HCURSOR WaitCursor;
  57.  
  58. /* miscellaneous functions */
  59.  
  60. static void Add2Buffer(char Chr)
  61. {/* add char to input buffer */
  62.  switch(Chr)
  63.    {case BS:
  64.       if(InBufLen>0)
  65.         {/* backup on screen */
  66.          DisplayChar(BS);
  67.          /* remove last char from buffer */
  68.          InBufLen--;
  69.         }
  70.       break;
  71.     default:
  72.       /* display char */
  73.       DisplayChar(Chr);
  74.       /* put into buffer */
  75.       if(InBufLen<MAX_BUF) InBuffer[InBufLen++] = Chr;
  76.       break;
  77.    }
  78. }
  79.  
  80. static void DisplayError(int Code, LPSTR Msg)
  81. {DisplayString("ERROR: ");
  82.  if(Msg) DisplayString(Msg);
  83.  if(Code)
  84.    {wilErrorText(Code,(LPSTR)Temp,50);
  85.     DisplayLine((LPSTR)Temp);
  86.    }
  87.  SetCursor(ArrowCursor);
  88. }
  89.  
  90. /* WinMain */
  91.  
  92. #ifdef WIN32
  93. int WINAPI
  94. #else
  95. int PASCAL
  96. #endif
  97. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  98.         USE_PTR szCmdLine,  int nCmdShow)
  99. {WNDCLASS  wc;
  100.  MSG msg;
  101.  BOOL Result;
  102.  if(!hPrevInstance)
  103.    {/* register main window class */
  104.     wc.style = CS_HREDRAW | CS_VREDRAW;
  105.     wc.lpfnWndProc = MainWndProc;
  106.     wc.cbClsExtra = 0;
  107.     wc.cbWndExtra = 0;
  108.     wc.hInstance = hInst;
  109.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  110.     wc.hCursor = NULL;
  111.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  112.     wc.lpszMenuName =  "HostMenu";
  113.     wc.lpszClassName = "HostWClass";
  114.     Result = RegisterClass(&wc);
  115.     if(!Result) return FALSE;
  116.    }
  117.  
  118.  /* create main window */
  119.  hInstance = hInst;
  120.  hMainWnd = CreateWindow(
  121.         "HostWClass",   "SFINGR",    WS_OVERLAPPEDWINDOW,
  122.         CW_USEDEFAULT,  CW_USEDEFAULT,
  123.         WinWidth,       WinHeight,
  124.         NULL,           NULL,
  125.         hInstance,      NULL);
  126.  ShowWindow(hMainWnd, nCmdShow);
  127.  UpdateWindow(hMainWnd);
  128.  hMenu = GetMenu(hMainWnd);
  129.  
  130.  /* window control loop */
  131.  
  132.  while(GetMessage(&msg,NULL,0,0))
  133.    {
  134.     TranslateMessage(&msg);
  135.     DispatchMessage(&msg);
  136.    }
  137.  return (msg.wParam);
  138. } /* end WinMain */
  139.  
  140. #ifdef WIN32
  141. LRESULT CALLBACK
  142. #else
  143. long FAR PASCAL
  144. #endif
  145. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  146. {int Code;
  147.  HDC hDC;
  148.  PAINTSTRUCT ps;
  149. #ifdef WIN32
  150. #else
  151.  static FARPROC lpfnAboutDlgProc;
  152. #endif
  153.  hMainWnd = hWindow;
  154.  switch (iMsg)
  155.     {case WM_CREATE:
  156.       /* create cursors */
  157.       ArrowCursor = LoadCursor(NULL, IDC_ARROW);
  158.       WaitCursor = LoadCursor(NULL, IDC_WAIT);
  159.       SetCursor(ArrowCursor);
  160. #ifdef WIN32
  161. #else
  162.        /* create thunk for Win16 */
  163.        lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  164. #endif
  165.       /* initialize paint module */
  166.       PaintInit();
  167.       /* attach WINSOCK */
  168.       DisplayString("Attaching WINSOCK...");
  169.       Code = wilAttach();
  170.       DisplayLine("OK");
  171.       if(Code<0) DisplayError(Code,"wilAttach fails:");
  172.       else
  173.         {wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
  174.          DisplayLine((LPSTR)Temp);
  175.          wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
  176.          DisplayLine((LPSTR)Temp);
  177.          wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
  178.          DisplayLine((LPSTR)Temp);
  179.         }
  180.       break;
  181.  
  182.      case WM_COMMAND:
  183.          switch(wParam)
  184.            {case MSG_ABOUT :
  185. #ifdef WIN32
  186.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  187. #else
  188.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  189. #endif
  190.                return 0;
  191.             case MSG_EXIT:
  192.               wilRelease();
  193.               DestroyWindow(hMainWnd);
  194.               break;
  195.  
  196.             case MSG_DEBUG:
  197.               Code = wilDebug(0);
  198.               wsprintf((LPSTR)Temp,"Debug(0) returned %d",Code);
  199.               DisplayLine((LPSTR)Temp);
  200.               break;
  201.  
  202.             case MSG_FINGER:
  203.               InBufLen = 0;
  204.               DisplayString("Enter user (usr@domain):");
  205.               break;
  206.  
  207.            }
  208.          break;
  209.  
  210.     case WM_PAINT:
  211.       HideCaret(hMainWnd);
  212.       hDC = BeginPaint(hMainWnd, &ps);
  213.       SetMapMode(hDC,MM_ANISOTROPIC);
  214.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  215.       PaintMain(hDC,&ps);
  216.       EndPaint(hMainWnd,&ps);
  217.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  218.       ShowCaret(hMainWnd);
  219.       break;
  220.  
  221.     case WM_DESTROY:
  222.       PostQuitMessage(0);
  223.       break;
  224.  
  225.     case WM_CHAR:
  226.       if(wParam==CR)
  227.         {/* do the CR */
  228.          DisplayChar((char)wParam);
  229.          /* user has completed input */
  230.          DisplayChar(LF);
  231.          InBuffer[InBufLen] = '\0';
  232.          /* execute command */
  233.          wsprintf((LPSTR)Temp,"User@Host: %s",(LPSTR)InBuffer);
  234.          DisplayLine((LPSTR)Temp);
  235.          /* extract user & host names */
  236.          Ptr = StringChar((LPSTR)InBuffer,'@');
  237.          if(Ptr==NULL)
  238.            {DisplayError(Code, "Cannot recognize User@Host");
  239.             break;
  240.            }
  241.          *Ptr = '\0';
  242.          lstrcpy((LPSTR)User, (LPSTR)InBuffer);
  243.          lstrcpy((LPSTR)Host, (LPSTR)(++Ptr));
  244.          wsprintf((LPSTR)Temp,"User ='%s'", (LPSTR)User);
  245.          DisplayLine(Temp);
  246.          wsprintf((LPSTR)Temp,"Host ='%s'", (LPSTR)Host);
  247.          DisplayLine(Temp);
  248.          /* put up hour glass */
  249.          SetCursor(WaitCursor);
  250.          /* ask for host by name */
  251.          Code = wilAskHostByName((LPSTR)Host);
  252.          if(Code<0)
  253.            {DisplayError(Code, NULL);
  254.             break;
  255.            }
  256.          /* get host name */
  257.          HostAddr = wilGetHostAddr(0);
  258.          if(HostAddr==0)
  259.            {DisplayError(0, "Cannot get IP addess");
  260.             break;
  261.            }
  262.          wsprintf((LPSTR)Temp,"HostAddr = %s\n", wilGetHostDotted(0));
  263.          DisplayLine((LPSTR)Temp);
  264.          /* create socket */
  265.          Socket = wilTcpSocket();
  266.          if((int)Socket<0)
  267.            {DisplayError((int)Code, NULL);
  268.             break;
  269.            }
  270.          /* attempt to connect to remote host */
  271.          Code = wilConnect(Socket,HostAddr,SFINGR_PORT);
  272.          if(Code<0)
  273.            {DisplayError(Code, NULL);
  274.             break;
  275.            }
  276.          /* wait up to 20 seconds for connection */
  277.          if(wilIsConnected(Socket,20000)) DisplayLine("Connected");
  278.          else
  279.            {DisplayError(0, "Cannot CONNECT");
  280.             break;
  281.            }
  282.          /* send user name */
  283.          wsprintf((LPSTR)Temp,"%s\r\n",(LPSTR)User);
  284.          Code = wilWriteString(Socket,(LPSTR)Temp);
  285.          if(Code<0)
  286.            {DisplayError(Code, NULL);
  287.             break;
  288.            }
  289.          /* read socket */
  290.          while(1)
  291.            {/* read next string */
  292.             if(!wilDataIsReady(Socket,TEN_SEC)) break;
  293.             Code = wilReadString(Socket,(LPSTR)InBuffer,MAX_BUF);
  294.             if(Code==WIL_EOF) break;
  295.             if(Code>0)
  296.               {wsprintf((LPSTR)Temp,"%s", (LPSTR)InBuffer);
  297.                DisplayString((LPSTR)Temp);
  298.               }
  299.            }
  300.          DisplayLine("");
  301.          wilCloseSocket(Socket);
  302.          SetCursor(ArrowCursor);
  303.         }
  304.       else
  305.         {/* add char to input buffer */
  306.          Add2Buffer((char)wParam);
  307.         }
  308.       break;
  309.  
  310.     default:
  311.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  312.    }
  313.  return 0;
  314.  
  315. } /* end MainWndProc */